package com.ifcar.app.api.dao.impl;

import com.ifcar.app.api.dao.AccountApi;
import com.ifcar.app.data.dao.AccountData;
import com.ifcar.app.data.dao.impl.AccountDataImpl;
import com.ifcar.app.util.Dict;
import com.ifcar.app.util.EC;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Valid;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.url.ajax.json.JSONObject;

/**
 * 业务层实现
 * @author nxlixa
 *
 */
public class AccountApiImpl implements AccountApi{
	
	/**
	 * 数据层接口
	 */
	private AccountData mDataDao;
	
	public AccountApiImpl(){
		mDataDao = new AccountDataImpl();
	}
	
	// 注册
	@Override
	public JSONObject register(String phone, String pwd, String email) throws Exception {
		
		JSONObject message = null;
		
		/**
		 * 缺少参数错误 手机号码和密码必须
		 */
		message = Valid.paramterMissValid(new String[]{phone, pwd});
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(phone)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		/**
		 * 密码长度验证
		 */
		if(!Valid.validPassword(pwd)){
			message.put(EC.ERROR, EC.MSG_PASSWORD_LENGTH_INVALID);
			message.put(EC.CODE, EC.CODE_PASSWORD_LENGTH_INVALID);
			return message;
		}
		
		/**
		 * 邮箱合法验证 不为空的情况下
		 */
		if(!StringUtil.isEmptyStr(email) && !Valid.validEmail(email)){
			message.put(EC.ERROR, EC.MSG_EMAIL_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_EMAIL_FORMAT_ERROR);
			return message;
		}
		
		/**
		 * 是否存在相同电话号码
		 */
		DBCursor dbCur = mDataDao.findUserObjByPhone(phone);
		if(null != dbCur && dbCur.count() > 0){
			message.put(EC.ERROR, EC.MSG_PHONE_ALREADY_EXIST);
			message.put(EC.CODE, EC.CODE_PHONE_ALREADY_EXIST);
			dbCur.close();
			return message;
		}
		dbCur = null;
		
		/**
		 * 是否存在相同邮箱账号
		 */
		dbCur = mDataDao.findUserObjByEmail(email);
		if(null != dbCur && dbCur.count() > 0){
			message.put(EC.ERROR, EC.MSG_EMAIL_ALREADY_EXIST);
			message.put(EC.CODE, EC.CODE_EMAIL_ALREADY_EXIST);
			dbCur.close();
			return message;
		}
		
		dbCur = null;
		
		WriteResult wr = mDataDao.register(phone, pwd, email);
		
		if(null == wr.getError()){
			message.put(EC.SUCCESS, EC.SUCCESS);
		} else {
			message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			message.put(EC.CODE, EC.CODE_SERVER_ERROR);
		}
		
		return message;
	}

	// 登录
	@Override
	public JSONObject login(String phone, String pwd) throws Exception {
		
		JSONObject message = null;
		
		/**
		 * 缺少参数错误 手机号码和密码必须
		 */
		message = Valid.paramterMissValid(new String[]{phone, pwd});
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(phone)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		
		/**
		 * 是否存在登录帐号
		 */
		DBCursor dbCur = mDataDao.findUserObjByPhone(phone);
		if(null == dbCur || dbCur.count() == 0){
			message.put(EC.ERROR, EC.MSG_PHONE_NOT_EXIST);
			message.put(EC.CODE, EC.CODE_PHONE_NOT_EXIST);
			dbCur.close();
			return message;
		}
		dbCur = null;
		
		dbCur = mDataDao.findUserObj4Login(phone, pwd);
		
		if(null != dbCur && dbCur.count() > 0){
			if(dbCur.hasNext()){
				DBObject dbObj = dbCur.next(); 
				if(null != dbObj){
					String account = (String) dbObj.get(Dict.PHONENUM);
					if(!StringUtil.isEmptyStr(account)){
						message.put(EC.SUCCESS, EC.MSG_LOGIN_SUCCESS);
						message.put(EC.MESSAGE, new JSONObject().put(Dict.PHONENUM, account));
						dbCur.close();
						dbCur = null;
						return message;
					}
				}
			}
		} 
		
		message.put(EC.ERROR, EC.MSG_PASSWORD_ERROR);
		message.put(EC.CODE, EC.CODE_PASSWORD_ERROR);
		
		return message;
	}
	
}
